﻿using Module.FlowChart.Model;
using System;
using System.IO;
using System.Windows.Forms;

namespace Module.FlowChart.View
{
    class ViewManager_Text : ViewManager
    {
        public override string TypeName => TextWorkPage.TypeNameS;
        TextBox text;
        public ViewManager_Text(ViewManagerHandles h, Document doc, TextWorkPage page, Control container) : base(h, doc, page, container)
        {
            text = new TextBox();
            text.Multiline = true;
            text.Dock = DockStyle.Fill;
            text.Parent = container;
            text.Text = "Test";
            text.SetOnTextEdited((s, e) =>
            {
                if (page.Str != text.Text)
                {
                    page.IsChanged = true; page.Str = text.Text;
                }
            });
        }

        public override void Resized() { }

        protected override void CreatMenu() { }

        protected override void InitMenuState(bool visible) { }
        private void BindCandidateList(bool active)
        {
            //ListView chartCandidateList = Handles.GetControl("chartCandidateList") as ListView;
            //chartCandidateList.Groups.Clear();
            //chartCandidateList.Items.Clear();
        }
        public override void SetActive(bool v)
        {
            base.SetActive(v);
            BindCandidateList(v);
            if (v)
            {
                text.Text = (Page as TextWorkPage).Str;
            }

        }
    }


}
namespace Module.FlowChart.Model
{

    class TextWorkPage : WorkPage
    {
        public const byte StartFlag = 110;//文件开始标志
        public const byte EndFlag = 120;//文件结束标志

        public override string TypeName => TypeNameS;

        static public string TypeNameS = "TextPage";
        static void StaticInit()
        {
            string tname = TypeNameS;
            WorkPageLoaders[tname] = (br) =>
            {
                TextWorkPage re = new TextWorkPage();
                re.LoadFromBytes(br);
                return re;
            };
            WorkPageCreators[tname] = (name) =>
            {
                return new TextWorkPage(name);
            };
            WorkPageNameCreator[tname] = (index) =>
            {
                return "Text" + index;
            };
        }


        public string Str;
        private TextWorkPage()
        {
            Str = "";
        }
        public TextWorkPage(string name) : base(name)
        {
            Str = "";
        }
        public override WorkPage LoadFromBytes(BinaryReader br)
        {
            if (br.ReadByte() != StartFlag)
                throw new Exception("Unexpected start");
            m_name = br.ReadString();
            Str = br.ReadString();

            if (br.ReadByte() != EndFlag)
                throw new Exception("Unexpected end");
            return this;
        }

        public override bool Save(BinaryWriter bw)
        {
            IsChanged = false;
            bw.Write(TypeName);
            bw.Write(StartFlag);
            bw.Write(m_name);
            bw.Write(Str);
            bw.Write(EndFlag);
            return true;
        }

        protected override void _OnChangeName(string from, string to)
        {
            throw new NotImplementedException();
        }
    }

}